//左侧列表展开事件
$("#fore-index .box-dl dt").click(function () {

    //取消首页的点击效果
    var index = $("#fore-index .box-line.selected");
    if (index.length > 0) {
        index.removeClass("selected");
        imgChange(index.children(".nav-icon")[0], "hide");
    }

    var that = $(this);
    //找到上一个被选中的按钮
    var dt_selected = $("#fore-index .box-dl dt.selected");

    //判断当前的按钮是否被选中
    if (that.hasClass("selected")) {
        that.removeClass("selected");
        imgClick(that, "hide");
        that.siblings("dd").hide(400);
    } else {
        //取消前一个被选中的选中状态
        if (dt_selected.length > 0) {
            $(dt_selected[0]).removeClass("selected");
            imgClick(dt_selected[0], "hide");
            $(dt_selected[0]).siblings("dd").hide(400);
        }

        //将当前的选中
        that.addClass("selected");
        imgClick(that, "show");
        that.siblings("dd").show(400);
    }

});

//左侧切换右侧内容按钮点击
$("#fore-index .box-dl dd").click(function () {
    var that = $(this);
    var page_name = that.attr("page-name");

    //需要登录的页面
    if(page_name === "purchase"){
        if(!isLogin){//如果没有登录
            show_login();
            return;
        }
    }

    //如果不是已经选中的按钮
    if (!that.hasClass("selected")) {
        //取消已经选中的
        var dd_selected = $("#fore-index .box-dl dd.selected");
        if (dd_selected.length > 0) {
            dd_selected.removeClass("selected");
            $(dd_selected.children(".dd-icon-right")[0]).addClass("layui-hide");
            imgChange(dd_selected.children(".dd-icon-left")[0], "hide");
        }

        //将自己选中
        that.addClass("selected");
        $(that.children(".dd-icon-right")[0]).removeClass("layui-hide");
        imgChange(that.children(".dd-icon-left")[0], "show");

    }


    refresh(page_name);//刷新指定内容
});

//首页点击效果
$("#fore-index .box-line").click(function () {

    refresh("/party/home/index");//右侧显示主页

    var that = $(this);

    if (!that.hasClass("selected")) {

        //显示自己按钮效果
        that.addClass("selected");
        imgChange(that.children(".nav-icon")[0], "show");

        //隐藏列表效果
        var dt_selected = $("#fore-index .box-dl dt.selected");
        if (dt_selected.length > 0) {
            $(dt_selected[0]).removeClass("selected");
            imgClick(dt_selected[0], "hide");
            var dd_selected = $("#fore-index .box-dl dd.selected");
            if (dd_selected.length > 0) {
                dd_selected.removeClass("selected");
                $(dd_selected.children(".dd-icon-right")[0]).addClass("layui-hide");
                imgChange(dd_selected.children(".dd-icon-left")[0], "hide");
            }
            $(dt_selected[0]).siblings("dd").hide(400);

        }
    }

});

//图片点击
function imgClick(obj, type) {
    var imgs = $(obj).children("img");
    for (var i = 0; i < imgs.length; i++) {
        imgChange(imgs[i], type);
    }
}

//对图片进行操作
function imgChange(img, type) {
    var src = $(img).attr("src");
    var num = 19;
    num = type === "show" ? num - 1 : num + 1;
    if (num < 10) num = "0" + num;
    src = "/static/images/party/img/nav_icon_" + num + ".png";
    $(img).attr("src", src);
}

//右侧页面局部刷新
function refresh() {
    var len = arguments.length;
    if (len < 1)
        return;

    var page_name = arguments[0];//页面名
    var params = {};
    if (len === 2) {
        params = arguments[1];
        if (!(params instanceof Object))
            params = eval('(' + params + ')');
    }
    $.post(page_name, params, function (result) {
        var fore_main = $("#fore-index .fore-main");
        fore_main.empty();
        fore_main.prepend(result);
    });
}

//加载动画
function loader(obj) {
    var s = '    <div class="main-loader">\n' +
        '        <div class="loader">\n' +
        '            <div class="line line1"></div>\n' +
        '            <div class="line line2"></div>\n' +
        '            <div class="line line3"></div>\n' +
        '        </div>\n' +
        '    </div>';
    $(obj).append(s);
}

//右侧导航刷新
$("#fore-index .fore-main").on("click", ".nav-name .re-fresh", function () {
    var for_url = $(this).attr("for");
    if (for_url !== "" && for_url !== null && for_url !== undefined) {
        var params = $(this).attr("params");
        refresh(for_url, params);
    }
});